package com.tcxhb.job.core.service.job.impl;

import com.alibaba.fastjson.JSONObject;
import com.tcxhb.job.dao.dataobject.JobInstanceDO;
import com.tcxhb.job.common.constants.JobApiConstants;
import com.tcxhb.job.core.service.job.AgentApiService;
import com.tcxhb.job.common.constants.CommonErrorCode;
import com.tcxhb.job.common.model.MiscResult;
import com.tcxhb.job.common.model.job.JobContext;
import com.tcxhb.job.common.service.HttpService;
import com.tcxhb.job.common.service.impl.HttpServiceImpl;
import com.tcxhb.job.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Auther: tcxhb
 * @Date: 2023/11/5
 */
@Component
@Slf4j
public class AgentApiServiceImpl implements AgentApiService {
    @Autowired
    private HttpClient client;
    private HttpService httpService;

    private HttpService getHttp() {
        if (httpService == null) {
            httpService = new HttpServiceImpl(client);
        }
        return httpService;
    }

    @Override
    public MiscResult<Boolean> run(JobInstanceDO instanceDO) {
        //对象
        try {
            JSONObject params = convertParams(instanceDO);
            String url = instanceDO.getMachineAddr() + JobApiConstants.cmd_run;
            String str = getHttp().post(url, params, null);
            return JsonUtils.toMiscBean(str, Boolean.class);
        } catch (Exception e) {
            log.error("agent-run-exp:" + instanceDO.getId(), e);
            return MiscResult.err(CommonErrorCode.HTTP_TO_AGENT_FAIL);
        }
    }

    @Override
    public MiscResult<Boolean> beat(String host, Integer port,String cmd) {
        try {
            String url = getAgent(host, port, cmd);
            String str = getHttp().get(url, null, null);
            return JsonUtils.toBean(str, MiscResult.class, Boolean.class);
        } catch (Exception e) {
            log.error("agent-beat-exp:" + host, e);
            return MiscResult.err(CommonErrorCode.HTTP_TO_AGENT_FAIL);
        }
    }

    @Override
    public MiscResult<Integer> status(JobInstanceDO instanceDO) {
        try {
            JSONObject params = convertParams(instanceDO);
            String url = instanceDO.getMachineAddr() + JobApiConstants.cmd_status;
            String str = getHttp().get(url, params, null);
            return JsonUtils.toBean(str, MiscResult.class, Integer.class);
        } catch (Exception e) {
            log.error("agent-status-exp:" + instanceDO.getMachineAddr(), e);
            return MiscResult.err(CommonErrorCode.HTTP_TO_AGENT_FAIL);
        }
    }

    /**
     * @param host
     * @param port
     * @param cmd
     * @return
     */
    private String getAgent(String host, Integer port, String cmd) {
        StringBuilder builder = new StringBuilder("http://");
        builder.append(host).append(":").append(port).append("/");
        builder.append(cmd);
        return builder.toString();
    }

    private JSONObject convertParams(JobInstanceDO instanceDO) {
        JobContext context = new JobContext();
        context.setTraceId(instanceDO.getTraceId());
        context.setInstanceId(instanceDO.getId());
        context.setHandler(instanceDO.getJobParams().getHandler());
        context.setParams(instanceDO.getJobParams().getParams());
        context.setJobType(instanceDO.getJobParams().getType());
        //转json
        String content = JsonUtils.toJson(context);
        JSONObject params = JsonUtils.toJson(content);
        return params;
    }
}
